package com.jyu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyu.reggie.common.R;
import com.jyu.reggie.dto.SetmealDto;
import com.jyu.reggie.entity.Category;
import com.jyu.reggie.entity.Dish;
import com.jyu.reggie.entity.Setmeal;
import com.jyu.reggie.service.CategoryService;
import com.jyu.reggie.service.SetmealDishService;
import com.jyu.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /*
     * @description: 保存套餐
     * @param setmealDto 
     * @return: com.jyu.reggie.common.R<java.lang.String>
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());
        log.info("setmealDao: {}",setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        Page<Setmeal> pageInfo = new Page<>();
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo, queryWrapper);

        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> dtoList = new ArrayList<>();

        for (Setmeal setmeal : records) {

            Category category = categoryService.getById( setmeal.getCategoryId());

            SetmealDto setmealDto = new SetmealDto();

            setmealDto.setCategoryName( category.getName() );

            BeanUtils.copyProperties(setmeal,setmealDto);

            dtoList.add(setmealDto);

        }
        setmealDtoPage.setRecords(dtoList);

        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids: {}",ids);

        setmealService.removeWithDish(ids);

        return R.success("删除成功");
    }

    /*
     * @description: 
     * @param setmeal 
     * @return: com.jyu.reggie.common.R<java.util.List<com.jyu.reggie.entity.Setmeal>>
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+ '_' + #setmeal.status")
    public R<List<Setmeal>> list( Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!= null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }


    @PostMapping("/status/{status}")
    public R<String> outOfStock( @PathVariable Integer status, Long [] ids ){
        System.out.println("==========================unsold==============================");
        for (Long  id : ids) {
            LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();

            updateWrapper.eq(Setmeal::getId,id);

            updateWrapper.set(Setmeal::getStatus,status != 0 ? 1:0);

            setmealService.update(updateWrapper);
        }
        return R.success("状态已变更");
    }

    @GetMapping("/{id}")
    public R<Setmeal> getone(@PathVariable Long id){
        log.info("id {}",id);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,Setmeal::getId,id);
        Setmeal setmeal = setmealService.getOne(queryWrapper);

        return R.success(setmeal);
    }

}
